from django.core.urlresolvers import reverse_lazy
from django.shortcuts import resolve_url
from django.views.generic import ListView, CreateView, UpdateView, DetailView, DeleteView
from accounts.models import AgentInstitution
from ghouse import settings
from accounts.forms import SearchForm, AgentInstitutionForm

__author__ = '侯志文'


class AmdminListView(ListView):
    model = AgentInstitution
    template_name = "accounts/admin/admin_list.html"
    paginate_by = settings.PAGE_SIZE

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(AmdminListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(AmdminListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {}
        form = self.form
        form.is_valid()
        if form.cleaned_data['name']:
            kwargs['name__contains'] = form.cleaned_data['name']
        return self.model.objects.filter(**kwargs)


class AgentInstitutionCreate(CreateView):
    form_class = AgentInstitutionForm
    model = AgentInstitution
    template_name = 'accounts/admin/admin_form.html'

    def get_success_url(self):
        return resolve_url('admin_list')


class AgentInstitutionUpdate(UpdateView):
    form_class = AgentInstitutionForm
    model = AgentInstitution
    template_name = 'accounts/admin/admin_form.html'

    def get_success_url(self):
        return resolve_url('admin_list')


class AgentInstitutionDetail(DetailView):
    model = AgentInstitution
    template_name = 'accounts/admin/admin_detail.html'


class AgentInstitutionDelete(DeleteView):
    model = AgentInstitution
    success_url = reverse_lazy('admin_list')
    template_name = "accounts/confirm_delete.html"



